﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HSDc.RingleFlow.Api.Service;
using HSDc.RingleFlow.Api.Kernel;
using HSDc.RingleFlow.Kernel.ProcessActivity;

namespace HSDc.RingleFlow.Kernel.Default
{
    public class RespositoryServiceImpl : IRepositoryService
    {
        DefaultProcessDefinition pd = new DefaultProcessDefinition();
        string IRepositoryService.RegisterProcessDefinition(string DefinitionXml)
        {
            throw new NotImplementedException();
        }

        IProcessDefinition IRepositoryService.GetProcessDefinition(string processDefinitionId)
        {
            List<string> values = pd.GetValueByID(processDefinitionId);
            ProcessDefinition _pd = new ProcessDefinition(values[0], values[1], "1", true, 20);
            InitialDefaultActivities(ref _pd);
            return _pd;
        }

        IProcessDefinition IRepositoryService.GetProcessDefinitionByName(string processDefinitionName)
        {
            List<string> values = pd.GetValueByName(processDefinitionName);
            ProcessDefinition _pd = new ProcessDefinition(values[0], values[1], "1", true, 20);
            InitialDefaultActivities(ref _pd);
            return _pd;
        }

        private void InitialDefaultActivities(ref ProcessDefinition pd)
        {
            Activity ac, ac2 = null;
            string _id = Guid.NewGuid().ToString();
            ac = new HumanActivity(_id, "主管審核", null, null);
            pd.AddActivity(ac);
            string _id2 = Guid.NewGuid().ToString();
            ac2 = new EndActivity(_id2, "End", null, null);
            Transition trans = new Transition(ac, ac2, null);
            ac.AddOutboundTransition(trans);
            pd.AddActivity(ac);
        }
    }

    internal class DefaultProcessDefinition
    {
        private Dictionary<int, string> _ids = new Dictionary<int,string>();
        private Dictionary<string, int> _idKeys = new Dictionary<string, int>();
        private Dictionary<int, string> _names = new Dictionary<int,string>();
        private Dictionary<string, int> _nameKeys = new Dictionary<string, int>();
        public DefaultProcessDefinition()
        {
            _ids.Add(1, "{DDC218B8-ED80-4F2F-BA96-EA6F9CF22729}");
            _idKeys.Add("{DDC218B8-ED80-4F2F-BA96-EA6F9CF22729}", 1);
            _names.Add(1, "記錄執行名義聲請");
            _nameKeys.Add("記錄執行名義聲請", 1);
            _ids.Add(2, "{62DEA29E-CAB3-436C-B028-F3ECF1D7DCFB}");
            _idKeys.Add("{62DEA29E-CAB3-436C-B028-F3ECF1D7DCFB}", 2);
            _names.Add(2, "維護法務追蹤歷程");
            _nameKeys.Add("維護法務追蹤歷程", 2);
            _ids.Add(3, "{13D8E963-1227-4A4D-9E11-6A4A69AB9DEF}");
            _idKeys.Add("{13D8E963-1227-4A4D-9E11-6A4A69AB9DEF}", 3);
            _names.Add(3, "審核案件");
            _nameKeys.Add("審核案件", 3);
        }
        public List<string> GetValueByID(string id)
        {
            List<string> values = new List<string>();
            int key;
            if (!_idKeys.TryGetValue(id, out key))
                key = 1;
            string value;
            _ids.TryGetValue(key, out value);
            values.Add(value);
            _names.TryGetValue(key, out value);
            values.Add(value);
            return values;
        }
        public List<string> GetValueByName(string name)
        {
            List<string> values = new List<string>();
            int key;
            if (!_nameKeys.TryGetValue(name, out key))
                key = 1;
            string value;
            _ids.TryGetValue(key, out value);
            values.Add(value);
            _names.TryGetValue(key, out value);
            values.Add(value);
            return values;
        }
    }
}
